import UserApi from "@/api/userApi";
//vuex存储在内存，刷新会丢失，因此token还是存在cookie中，其他状态存在vuex中
import { getToken, setToken } from "@/libs/util";

export default {
  state: {
    userId: "",
    roleId: "",
    nickName: "",
    phone: "",
    mail:"",
    token: getToken(),
    access: "",
    //是否已获取了用户信息，路由跳转前，若发现尚未获取用户信息，将根据token再次获取一次
    hasGetInfo: false,
    router_from: "",
  },
  getters: {
    token: (state) => state.token,
    getPhone: (state) => state.phone,
    getMail: state => state.mail,
    getNickName: (state) => state.nickName,
    getRoleId: (state) => state.roleId,
  },
  mutations: {
    setUserId(state, id) {
      state.userId = id;
    },
    setRoleId(state, roleId) {
      state.roleId = roleId;
    },
    setPhone(state, phone) {
      state.phone = phone;
    },
    setMail(state, mail){
      state.mail = mail;
    },
    setNickName(state, name) {

      state.nickName = name;
    },
    setAccess(state, access) {
      state.access = access;
    },
    setToken(state, token) {
      //同时设置vuex以及cookie
      state.token = token;
      setToken(token);
    },
    setHasGetInfo(state, status) {
      state.hasGetInfo = status;
    },
  },
  actions: {
    // 登录
    handleLogin({ commit }, loginParam) {
      return new Promise((resolve, reject) => {
        UserApi.login(loginParam.phone, loginParam.password)
          .then((res) => {
            const data = res.data.data.userInfo.user;
            commit("setToken", res.data.data.userInfo.token);
            console.log("权限：" + data.accessList);
            commit("setAccess", data.accessList);
            resolve();
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    handleLoginE({ commit }, loginParam) {
      
      return new Promise((resolve, reject) => {
        UserApi.loginE(loginParam.mail, loginParam.password)
          .then((res) => {
            const data = res.data.data.userInfo.user;
            commit("setToken", res.data.data.userInfo.token);
            console.log("权限：" + data.accessList);
            commit("setAccess", data.accessList);
            resolve();
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    // 退出登录
    handleLogOut({ state, commit }) {
      return new Promise((resolve, reject) => {
        UserApi.logout(state.token)
          .then(() => {
            commit("setToken", "");
            commit("setAccess", []);
            resolve();
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    // 获取用户相关信息
    getUserInfo({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          UserApi.getUserInfo(state.token)
            .then((res) => {
              const data = res.data.data.user;
              //console.log("user 信息：" + JSON.stringify(data))
              commit("setNickName", data.nickName);
              commit("setPhone", data.phone);
              commit("setMail", data.mail);
              commit("setUserId", data.id);
              commit("setRoleId", data.roleId);
              commit("setAccess", data.accessList);
              commit("setHasGetInfo", true);
              resolve(data);
            })
            .catch((err) => {
              reject(err);
            });
        } catch (error) {
          reject(error);
        }
      });
    },
  },
};
